package com.study.config.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhuzi
 * 2024/12/2 10:43
 */
@Slf4j
public class ContentCachingFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        filterChain.doFilter(requestWrapper, response);
        // 确保请求体被缓存
        System.out.println(StringUtils.center("过滤器", 51, "*"));

        System.out.println(request.getRequestURI());
        System.out.println(request.getMethod());
        requestWrapper.getContentAsByteArray();
        System.out.println(StringUtils.center("-", 51, "*"));
    }
}
